// pages/content/content.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getfocus:true,
    showidea:"",
    chooseSize: false,
    animationData: {},
    animationDatat: {},
    indicatorDots: false,  //小点
    autoplay: true,    //是否自动轮播
    interval: 5000,    //间隔时间
    duration: 5000,    //滑动时间
    content:[],        //接受传过来的数据
    praisenum:0,       //接受点赞数的变量
    commentnum:1,      //接受评论数的变量
    collectionnum:0,   //接受收藏数的变量
    showpraise:true,      //显示是否点赞图标的变量
    showcollection:true,  //显示是否收藏图标 
    praisedata:0,         //判断是否需要往数据库更新内容
    collectiondata:0,      //判断是否需要向数据库更新内容
    backcomment:true,
    commentcontent:[],     //接收传过来的评论数据
   
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //console.log('游记id:'+app.globalData.content_item.sid)
    that.setData({
      content: app.globalData.content_item,
      praisenum: app.globalData.content_item.is_like,
      commentnum: app.globalData.content_item.scost
    })

   
   
    //获取评论
    wx:wx.request({
      url: app.globalData.url +'/v1/Comment/getCommentListBySid?sid='+app.globalData.content_item.sid,
      data: '',
      header: {
        "token": app.globalData.alltoken
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res.data.data)
        if (res.data.msg == '获取游记评论信息为空'){
          that.setData({
            commentnum:0
          })   
        }else{
          that.setData({
            commentnum: res.data.data.data.length,
            commentcontent:res.data.data.data
          })
          console.log(that.data.commentcontent)
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })

    //点赞
    wx:wx.request({
      url: app.globalData.url + '/v1/Praise/getPraiseDetailBySid?sid=' + app.globalData.content_item.sid,
      data: '',
      header: {
        "token": app.globalData.alltoken
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res)
        if(res.data.data.praiseByUid.praise_type == '喜欢'){
          //获取当前用户是否点赞过
          that.setData({
            showpraise: false,
            praisenum: res.data.data.praiseLikeCount
          })
        } else{
          that.setData({
            showpraise: true,
            praisenum: res.data.data.praiseLikeCount
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })




   
    

    //获取当前用户是否收藏过
    that.setData({
      showcollection: true
    })
  },

  //点击赞
  onClickpraise: function (e) { 
    var that = this
    if (that.data.showpraise){
      
      that.setData({
        showpraise: !that.data.showpraise,
        praisenum: that.data.praisenum+1,
        praisedata: that.data.praisedata+1
      })
      console.log(that.data.showpraise + "..." + that.data.praisenum + "..." + that.data.praisedata)
    }else{
     
      that.setData({
        showpraise: !that.data.showpraise,
        praisenum: that.data.praisenum - 1,
        praisedata: that.data.praisedata - 1
      })
      
      console.log(that.data.showpraise + "..." + that.data.praisenum + "..." + that.data.praisedata)
    }

  },

  //点击收藏
  onClickcollection: function (e) {
    var that = this
    if (that.data.showcollection) {
      that.setData({
        showcollection: !that.data.showcollection,
        collectionnum: that.data.collectionnum + 1,
        collectiondata: that.data.collectiondata + 1
      })
      console.log(that.data.showcollection + "..." + that.data.collectionnum + "..." + that.data.collectiondata)
    } else {
      that.setData({
        showcollection: !that.data.showcollection,
        collectionnum: that.data.collectionnum - 1,
        collectiondata: that.data.collectiondata - 1
      })
      console.log(that.data.showcollection + "..." + that.data.collectionnum + "..." + that.data.collectiondata)
    }

  },

//点击弹出评论
  chooseSezi: function (e) {
    // 用that取代this，防止不必要的情况发生
    var that = this;
    // 创建一个动画实例
    var animation = wx.createAnimation({
      // 动画持续时间
      duration: 500,
      // 定义动画效果，当前是匀速
      timingFunction: 'linear'
    })
    // 将该变量赋值给当前动画
    that.animation = animation
    // 先在y轴偏移，然后用step()完成一个动画
    animation.translateY(200).step()
    // 用setData改变当前动画
    that.setData({
      // 通过export()方法导出数据
      animationData: animation.export(),
      // 改变view里面的Wx：if
      chooseSize: !that.data.chooseSize
    })
    // 设置setTimeout来改变y轴偏移量，实现有感觉的滑动
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 200)
  },

 


  closecomment:function(){
    var that = this
    that.setData({
      chooseSize: !that.data.chooseSize
    })
  },





//隐藏回复界面
  backcomment:function(){
    var that = this
    that.setData({
      backcomment: true
    })
  },

//判断长按单击时间
  handleTouchStart:  function (e)  {
        this.startTime  =  e.timeStamp;
        //console.log(" startTime = " + e.timeStamp);  
  },

  //touch end
  handleTouchEnd:  function (e)  {
        this.endTime  =  e.timeStamp;
        //console.log(" endTime = " + e.timeStamp);  
  },  


  handleClick:  function (e)  {
        //console.log("endTime - startTime = " + (this.endTime - this.startTime));    
        if  (this.endTime  -  this.startTime  <  350)  {
          var that = this
          that.setData({
            backcomment: false
          });
        }
  },

  handleLongPress:  function (e)  {
    //console.log("endTime - startTime = " + (this.endTime - this.startTime));
    console.log("长按");
  },

  



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    console.log(that.data.praisedata)
    if (that.data.praisedata == 1) {
      wx:wx.request({
        url: app.globalData.url +'/v1/Praise/likePraise?sid='+app.globalData.content_item.sid,
        data: '',
        header: {
          "token": app.globalData.alltoken
        },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function(res) {
          console.log(res)
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    } else if (that.data.praisedata == -1) {
      wx: wx.request({
        url: app.globalData.url + '/v1/Praise/deletePreise?sid=' + app.globalData.content_item.sid,
        data: '',
        header: {
          "token": app.globalData.alltoken
        },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res)
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    console.log("我走了，上传吧" + that.data.praisedata + "..." + that.data.collectiondata)

  },
//上传评论
  commentaction:function(e){
    var that = this
    var formData = e.detail.value;
    wx:wx.request({
      url: app.globalData.url + '/v1/Comment/saveComment',
      data: {
        "umessage": formData,
        "sid": app.globalData.content_item.sid,
        "touid": app.globalData.content_item.uid,
        "comment_type": "评论"
      },
      header: {
        'Content-Type': 'application/json',
        "token": app.globalData.alltoken
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res)
        that.setData({
          showidea: "",
        })

        wx: wx.request({
          url: app.globalData.url + '/v1/Comment/getCommentListBySid?sid=' + app.globalData.content_item.sid,
          data: '',
          header: {
            "token": app.globalData.alltoken
          },
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            console.log(res.data.data)
            if (res.data.msg == '获取游记评论信息为空') {
              that.setData({
                commentnum: 0
              })
            } else {
              that.setData({
                commentnum: res.data.data.data.length,
                commentcontent: res.data.data.data
              })
              console.log(that.data.commentcontent)
            }
          },
          fail: function (res) { },
          complete: function (res) { },
        })

      },
      fail: function(res) {},
      complete: function(res) {},
    })


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})